using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;


namespace UrduCtrl
{
	/// <summary>
	/// Summary description for UrduListBox.
	/// </summary>
	public class UrduListBox : System.Windows.Forms.ListBox
	{
		

		#region Methods to access base class items...

		private void DoSetElement(int index, string value)
		{
			base.Items[index] = value;
		}

		private string DoGetElement(int index)
		{
			return (string) base.Items[index];
		}

		private IEnumerator DoGetEnumerator()
		{
			return base.Items.GetEnumerator();
		}

		private int DoGetItemCount()
		{
			return base.Items.Count;
		}

		private int DoInsertItem(int index, string item)
		{
//			item.imageList = this.imageList;
//			item.itemIndex = index;
			base.Items.Insert(index, item);
			return index;
		}

		private void DoRemoveItem(int index)
		{
			base.Items.RemoveAt(index);
		}

		private void DoClear()
		{
			base.Items.Clear();
		}

		#endregion

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private ListBox.ObjectCollection listItems=null;

		public UrduListBox()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			listItems= new ListBox.ObjectCollection(this);
			//listItems.Add("Test");
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			

			components = new System.ComponentModel.Container();

			this.Font = new System.Drawing.Font("Urdu Naskh Asiatype", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
			this.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			
		}
		#endregion

		protected override void OnPaint(PaintEventArgs pe)
		{
			// TODO: Add custom paint code here

			// Calling the base class OnPaint
			base.OnPaint(pe);
		}

		/// <summary>
		/// The items in the list box
		/// </summary>
//		[Category("Behavior")]  
//		[Description("The items in the list box")]
//		[Localizable(true)]
//		[MergableProperty(false)]
//		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
//		[Editor(typeof(UrduListPropertyEditor), typeof(UITypeEditor))]
		

//		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
//		[Category("Appearance"), 
//		Description("The items in the list box."), 
//		Editor(typeof(UrduListPropertyEditor), typeof(UITypeEditor))]

		[Category("Behavior")]  
		[Description("The items in the list box")]
		[Localizable(true)]
		[MergableProperty(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[Editor(typeof(UrduListPropertyEditor), typeof(UITypeEditor))]
		new public ListBox.ObjectCollection Items
		{
			get 
			{ 
				this.Invalidate();
				return this.listItems; 
			}
		}

	}

	public class UrduListPropertyEditor: UITypeEditor
	{

		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			if (context!=null && provider!=null)
			{
				IWindowsFormsEditorService edSrv= (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
				if (edSrv!=null)
				{					
					UrduTextListEditorForm dialog= new UrduTextListEditorForm();
					if (value is ListBox.ObjectCollection)
					{
						//MessageBox.Show("Test");

						ArrayList larray= new ArrayList((ListBox.ObjectCollection) value);
//						this.txtListItems.Lines = (string[]) larray.ToArray(typeof(string));
//						m_sList= (ListBox.ObjectCollection) value;
						dialog.Lines= (string[]) larray.ToArray(typeof(string));	
					}
					if (edSrv.ShowDialog(dialog)==System.Windows.Forms.DialogResult.OK)
					{
						//MessageBox.Show("before");
						((ListBox.ObjectCollection) value).Clear();
						((ListBox.ObjectCollection) value).AddRange(dialog.Lines);
						//value= dialog.Lines;
						//MessageBox.Show((String) value);
						
					}
					else
						MessageBox.Show("Failed!");
					dialog.Dispose();
					dialog= null;
				}
			}
			return value;
		}

		public override System.Drawing.Design.UITypeEditorEditStyle 
			GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) 
		{ 
			// We will use a window for property editing. 
			return UITypeEditorEditStyle.Modal; 
		}

		public override bool GetPaintValueSupported( 
			System.ComponentModel.ITypeDescriptorContext context) 
		{ 
			// No special thumbnail will be shown for the grid. 
			return false; 
		} 
	}
}
